package com.caojing.haolemai.ui.category.categoryrightfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseFragment;
import com.caojing.haolemai.bean.categorybean.BrandCategoryBean;
import com.caojing.haolemai.bean.categorybean.CategoryResultsBean;
import com.caojing.haolemai.ui.category.categoryadapter.CategoryLightHeaderAdapter;
import com.caojing.haolemai.ui.category.categoryadapter.CategoryRightListAdapter;
import com.caojing.haolemai.ui.category.categorydclickactivity.CategoryClickActivity;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/10/18.
 */
public class CategoryRightFragment extends BaseFragment {


    @Bind(R.id.category_right_list)
    ListView categoryRightList;

    private View headView;
    private RecyclerView recyclerView;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category_right;
    }

    @Override
    protected void initView(View view) {
        // LayoutInflater:布局填充器,将xml文件--->view类型.
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        // 填充视图的方法
        headView = inflater.inflate(R.layout.fragment_category_right_header, null);
        recyclerView = (RecyclerView) headView.findViewById(R.id.recycler_category_header);
        // 添加头部视图修改到下方
//        categoryRightList.addHeaderView(headView);

    }


    @Override
    protected void initData() {
        Bundle arguments = getArguments();
        final Serializable brandCategory = arguments.getSerializable("brandCategory");
        Serializable secondBeen = arguments.getSerializable("secondBeen");

        LinearLayoutManager manger=new LinearLayoutManager(getActivity());
        manger.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manger);
        if (brandCategory!=null){
            CategoryLightHeaderAdapter recycleradapter =new CategoryLightHeaderAdapter(getActivity(), (List<BrandCategoryBean>) brandCategory);
            assert recyclerView!=null;
            recyclerView.setAdapter(recycleradapter);

            recycleradapter.setOnRecyclerViewItemClickListener(new CategoryLightHeaderAdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClickListener(View view, int position) {


                    String brandId = ((List<BrandCategoryBean>) brandCategory).get(position).getBrandId();
                    String categoryId = ((List<BrandCategoryBean>) brandCategory).get(position).getCategoryId();
                    String brandName = ((List<BrandCategoryBean>) brandCategory).get(position).getBrandName();
                    Intent intent = new Intent(getActivity(), CategoryClickActivity.class);
                    intent.putExtra("brandName",brandName);
                    intent.putExtra("brandId",brandId);
                    intent.putExtra("categoryId",categoryId);
                    getActivity().startActivity(intent);

                }
            });
        }
        // 添加头部视图
        categoryRightList.addHeaderView(headView);
        if (secondBeen!=null){
            CategoryRightListAdapter listAdapter = new CategoryRightListAdapter(getActivity(), (List<CategoryResultsBean.SecondBean>) secondBeen);
            categoryRightList.setAdapter(listAdapter);
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
